package LangDet;

import java.util.ArrayList;

import com.cybozu.labs.langdetect.Detector;
import com.cybozu.labs.langdetect.DetectorFactory;
import com.cybozu.labs.langdetect.LangDetectException;
import com.cybozu.labs.langdetect.Language;

public class LangDetect {

	public void init(String profileDirectory) throws LangDetectException {
		DetectorFactory.loadProfile(profileDirectory);
	}

	public String detect(String text) throws LangDetectException {
		Detector detector = DetectorFactory.create();
		detector.append(text);
		return detector.detect();
	}

	public ArrayList<Language> detectLangs(String text) throws LangDetectException {
		Detector detector = DetectorFactory.create();
		detector.append(text);
		return detector.getProbabilities();
	}

	public ArrayList<String> filterComments(ArrayList<String> comments) {
		ArrayList<String> commentsFiltered = new ArrayList<String>();
		for  (String s : comments) {
			if(!s.isEmpty() && (s.matches(".*[A-Za-z].*"))){
				try {
					if (this.detect(s.concat(".")).equals("en")) {
						//System.out.println(s);
						//System.out.println(this.detect(s.concat(".")));
						commentsFiltered.add(s);
					}
				} catch (LangDetectException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return commentsFiltered;
	}
}
